<?php
/**
 * beimuaihui System
 * Copyright(c) 2011-2020 beimuaihui.
 * @license    http://www.gnu.org/licenses/gpl.html     This software Under GPL V3 License 
 * beimuaihui@gmail.com
 * http://code.google.com/p/beimuaihui/
 * $Id: Translate.php 262 2011-07-23 15:51:14Z beimuaihui $
 */


Zend_Loader::loadClass("Baogg_Db_Table");


class Translate  extends Baogg_Db_Table {
	/**
	 * The default table name 
	 */
	protected $_name = 'multilang_translate';
	
	function __construct() {
		$this->_name =   'multilang_translate';
		$this->_myPrimary=$this->_primary= "multilang_translate_id";
		
		parent::__construct();
	}
	
	function getList() {
		global $db,$config,$multiLang;		
		//temp variables
		$g_admin_language_id=1;
		//get session info for this page
		$session_prefix = getPageName();

		//$translate_lang_id = $_COOKIE["s_admin_{$session_prefix}_language_id"];

		if (isset($_REQUEST['language_id']))
		{
			$_SESSION["s_admin_{$session_prefix}_language_id"] = trim($_REQUEST['language_id']);
		} 
		$translate_lang_id = $_SESSION["s_admin_{$session_prefix}_language_id"];
		
		if(!$translate_lang_id )
		{	
			$translate_lang_id=1;
		}

		
		if ($_GET['dir']=="ASC" || $_GET['dir']=="DESC" ) {
			if ($_GET['sort']) {
				$orderby = " order by  ".$_GET['sort'] ." ".$_GET['dir'];	
			}	
		}

		$sql_count = "select count(*) as total 
						from  module_permission mp,multilang_table mut ,multilang_info mui 
						where  mp.module_permission_id=mui.key_id	
							and mui.multilang_table_id=mut.multilang_table_id
							and mui.language_id={$g_admin_language_id}  
							and mp.state=1					
							and mut.table_name='module_permission'
							and mp.module_permission_type in (0,1) 
							$condition ";	
		//echo $sql_count;
		
		$stmt='';     $stmt=$db->query($sql_count);
		$rs_count =$stmt->fetchAll();
		$rows = $rs_count[0]["total"];
		$pager = getPagerParam($session_prefix);
		$sql = "select  mp.module_permission_id module_permission_id,mui.content permission_name
					from  module_permission mp,multilang_table mut ,multilang_info mui 
					where  mp.module_permission_id=mui.key_id		
						and mui.multilang_table_id=mut.multilang_table_id
						and mui.language_id={$g_admin_language_id}  
						and mut.table_name='module_permission'
						and mp.state=1					
						and mp.module_permission_type in (0,1) 
					$condition  $orderby limit $pager->_offsize,$pager->_limit";
		// 
		//echo $sql;
		
		$stmt='';     $stmt=$db->query($sql);
		$moduleRS =$stmt->fetchAll();

		foreach ((array)$moduleRS as $key => $value) {
			$moduleArr['module_permission_id']=$value['module_permission_id'];
			$moduleArr['permission_name']=$value['permission_name'];
			//if (strlen($value['module_permission_id']) == 3) {
				$countSql="select  count(*) as varCount from multilang_content, multilang_translate  ".
					" where  multilang_translate.multilang_content_id=multilang_content.multilang_content_id  ".
					" and multilang_translate.language_id=1  ".
					" and multilang_content.module_permission_id  in 
						(select module_permission_id from module_permission 
						where module_permission_pid ='{$value['module_permission_id']}' 
						or module_permission_id ='{$value['module_permission_id']}') "; 
			//}else{
			
			//    $countSql="select  count(*) as varCount from multilang_content, multilang_translate  ".
			//        " where  multilang_translate.multilang_content_id=multilang_content.multilang_content_id  ".
			//        " and multilang_translate.language_id=1  ".
			//        " and multilang_content.module_permission_id='{$value['module_permission_id']}' ";
			//}
			//echo $countSql;			
			
			$stmt='';     $stmt=$db->query($countSql);
			$countRS =$stmt->fetchAll();

			if ($countRS)
				$moduleArr['varCount']=$countRS[0]['varCount'];
			else
				$moduleArr['varCount']=0;

			if (strlen($value['module_permission_id']) == 3) {		 // is parent module, get child's count
				$countSql="select  count(*) as varCount2 from multilang_content, multilang_translate  ".
					" where  multilang_translate.multilang_content_id=multilang_content.multilang_content_id  ".
					" and multilang_translate.language_id={$translate_lang_id}  ".
					" and multilang_content.module_permission_id  in 
						(select module_permission_id from module_permission
						where module_permission_pid ='{$value['module_permission_id']}' 
						or module_permission_id ='{$value['module_permission_id']}') "; 
			}else{		
				$countSql="select  count(*) as varCount2 from multilang_content, multilang_translate  ".
					" where  multilang_translate.multilang_content_id=multilang_content.multilang_content_id  ".
					" and multilang_translate.language_id={$translate_lang_id}  ".
					" and multilang_content.module_permission_id='{$value['module_permission_id']}' ";
			}
			
			/*$countSql="select  count(*) as varCount2 from multilang_content, multilang_translate  ".
				" where multilang_translate.multilang_content_id=multilang_content.multilang_content_id  ".
				" and multilang_translate.language_id={$translate_lang_id}  ".
				" and multilang_content.module_permission_id='{$value['module_permission_id']}' ";*/
			//echo $countSql;		
			
			$stmt='';     $stmt=$db->query($countSql);
			$countRS =$stmt->fetchAll();
			if ($countRS)
				$moduleArr['varCount2']=$countRS[0]['varCount2'];
			else
				$moduleArr['varCount2']=0;
		
			//$moduleArr['action']="<a  href='#'  onclick='checkLang(\"translateedit_list.php?\",\"{$value['module_permission_id']}\")' >{$multiLang->g_view}</a>";
			$moduleArr['action']=backendActionLink($multiLang->g_view,"list_view_link.gif","","#"," onclick='checkLang(\""."{$config['web']['baseurl']}multilang/translate/content/?\",\"{$value['module_permission_id']}\")' ");		
			$moduleData[]=$moduleArr;
		} 

		$listData=array(
			"total"=>$rows,
			"rs"=>$moduleData
		);
		return $listData;		
	}

	/*
	 * Content List
	 */
	function getContentList() {
		global $db,$config;		
		$session_prefix = getPageName();

		$tran_language_id=$_GET['language_id'];
		$tran_module_permission_id=$_GET['module_permission_id'];

		if(!$tran_language_id )	{	
			$tran_language_id=1;
		}	
		if (isset($_REQUEST['search'])){
			$_SESSION["s_admin_{$session_prefix}_content"] = trim($_REQUEST['search']);
		} 
		$s_keyword = $_SESSION["s_admin_{$session_prefix}_content"];
		
		if($s_keyword)	{
			$condition .= " and ( t2.content like '%{$s_keyword}%' or t3.content like '%{$s_keyword}%' ) ";	
		}
		
		
		if (isset($_GET['module_permission_id'])){
			if ($tran_module_permission_id!="all") {
				//$condition .= " and t1.module_permission_id= '{$tran_module_permission_id}' ";
				//if (strlen($tran_module_permission_id)==3)
					$condition .= " and t1.module_permission_id in (select module_permission_id from module_permission where module_permission_pid ='{$tran_module_permission_id}' or module_permission_id ='{$tran_module_permission_id}') "; 
				//else
					//$condition .= " and t1.module_permission_id= '{$tran_module_permission_id}' ";	
			}			
		} 
		
		$orderby = " order by ";
		if ($_GET['dir']=="ASC" || $_GET['dir']=="DESC" ){
			$orderby.=$_GET['sort']." ".$_GET['dir'].",";		
		}
		$orderby.= " con_updated_time desc  ";		

		$sql_count ="select  count(*) as total from multilang_content t1 left join multilang_translate t2 ".
					" on  t1.multilang_content_id=t2.multilang_content_id and t2.language_id=1 ".
					" left join  multilang_translate t3 ".
					" on  t1.multilang_content_id=t3.multilang_content_id  and t3.language_id={$tran_language_id} ".
					" where 1=1 $condition ";	

		
		//echo $sql_count;
		//exit();
		$stmt='';     $stmt=$db->query($sql_count);
		$rs_count =$stmt->fetchAll();
		$rows = $rs_count[0]["total"];

		$pager = getpagerparam ( $session_prefix );
		$sql_limit = " limit $pager->_offsize,$pager->_limit ";
		$sql = "select t2.content content_en,t3.content content_tran,
						t1.content_type content_type,
						t6.content permission_name,
						t2.updated_time con_updated_time ,
						t3.updated_time tran_updated_time ,
						t1.multilang_content_id multilang_content_id
						from multilang_content   t1 left join multilang_translate   t2 
						on  t1.multilang_content_id=t2.multilang_content_id and t2.language_id=1
						left join  multilang_translate t3 	on  t1.multilang_content_id=t3.multilang_content_id  and t3.language_id={$tran_language_id} 
						left join module_permission  t4 on  t4.module_permission_id=t1.module_permission_id						
						left join multilang_table  t5 on  t5.table_name='module_permission' 
						left join multilang_info  t6 on  t6.multilang_table_id=t5.multilang_table_id  and t6.key_id=t4.module_permission_id  and  t6.language_id=1
						where  1=1  						
						$condition   $orderby  $sql_limit";
					

		//echo $sql;
		//exit;
		
		$stmt='';     $stmt=$db->query($sql);
		$rs =$stmt->fetchAll();

		if(is_array($rs)){
			foreach ((array)$rs as $key => $value) {	
				//$value['action']="<a  href=translate_tran.php?module_permission_id={$value['module_permission_id']}>Translate</a>";
				$rs[$key]['content_type']=$config['global']['g_multilang_contenttype'][$rs[$key]['content_type']];		
				$rs[$key]['con_updated_time']=showTime($rs[$key]['con_updated_time'],1);
				$rs[$key]['tran_updated_time']=showTime($rs[$key]['tran_updated_time'],1);
			} 
		}else{
			$rs=array();
		}

		$listData=array(
			"total"=>$rows,
			"rs"=>$rs
		);
		return $listData;		
	}
	

	function delete($id)
	{		
		$sql_pre="DELETE multilang_content,multilang_translate " .
			" from multilang_translate LEFT JOIN multilang_content ".
			" ON multilang_content.multilang_content_id=multilang_translate.multilang_content_id  ".
			" WHERE multilang_translate.multilang_content_id=";
		$rows_affected=$this->db->query($sql_pre.$id);

		return $rows_affected;
	}
	


}
